package com.mwyq.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mwyq.dao.NewsMapper;
import com.mwyq.dao.TopicDocumentRelationMapper;
import com.mwyq.dao.TopicMapper;
import com.mwyq.model.News;
import com.mwyq.model.NewsExample;
import com.mwyq.model.Page;
import com.mwyq.model.Topic;
import com.mwyq.model.TopicDocumentRelation;
import com.mwyq.model.TopicDocumentRelationExample;
import com.mwyq.model.TopicExample;
import com.mwyq.model.TopicExample.Criteria;

@Service
public class TopicService {

	
	@Autowired
	public TopicMapper topicMapper;
	
	@Resource
	public TopicDocumentRelationMapper documentRelationMapper;
	
	@Resource
	public NewsMapper newsMapper;
	
	/**
	 * 显示所有话题,无时间，无热度
	 * */
	public List<Topic> getAllTopic(){
		TopicExample topic = new TopicExample();
		
		List<Topic> topic2 = topicMapper.selectByExample(topic);
		
		return topic2;
	}
	
	/**
	 * 首页显示时间排序的话题 
	 * 
	 * */
	public List<Topic> getHotTopics() {
		// TODO Auto-generated method stub
		return topicMapper.getHotTopics();
	}

	/**
	 * 获取每页的数量
	 * */
	public List<Topic> getTopicByPage(Page page) {
		// TODO Auto-generated method stub
		return topicMapper.getTopics(page.getBeginIndex(), page.getPageSize());
	}
	
	public Topic getTopic(String topic_id) {
		// TODO Auto-generated method stub
		return topicMapper.selectByPrimaryKey(topic_id);
	}

	public int getTopicNum() {
		// TODO Auto-generated method stub
		return topicMapper.selectCount();
	}
	
	/**
	 * 获取话题对应新闻数量
	 * */
	public int getNewsNumByTopicid(String topicid){
		TopicDocumentRelationExample example = new TopicDocumentRelationExample();
		TopicDocumentRelationExample.Criteria criteria = example.createCriteria();
		criteria.andTopic_idEqualTo(topicid);
		documentRelationMapper.selectByExample(example);
		int num =documentRelationMapper.countByExample(example);
//		System.out.println("TopicNewsNum---"+num);
		return num;
	}
	
	/**
	 * 获取话题对应relation最大的新闻newsid
	 * */
	public int getNewsidByTopicid(String topicid){
		TopicDocumentRelationExample example = new TopicDocumentRelationExample();
		TopicDocumentRelationExample.Criteria criteria = example.createCriteria();
		criteria.andTopic_idEqualTo(topicid);
		example.setOrderByClause(String.valueOf("relation"));//对relation进行排序
//		System.out.println("example---"+example);
		documentRelationMapper.selectByExample(example);
		//获取所有的topicid对应用的条目信息
		List<TopicDocumentRelation> topicNewsRelation = documentRelationMapper.selectByExample(example);
//		System.out.println("topicNewsRelation---"+topicNewsRelation);
		while(topicNewsRelation.size()!=0){
			TopicDocumentRelation first = topicNewsRelation.get(topicNewsRelation.size()-1);//取最后一条数据，最大relation
			int newsid = first.getNews_id();//得到第一条数据的newsid
//			System.out.println("newsid---"+newsid);
			return newsid;
		}
		return 0;
	}
	
	/**
	 *获取topic相关性最大对应的新闻
	 * **/
	public List<News> getTopicNews(String topicid){
		int newsid = getNewsidByTopicid(topicid);
		while(newsid!=0){
			NewsExample newsExample = new NewsExample();
			NewsExample.Criteria criteria = newsExample.createCriteria();
			criteria.andNews_idEqualTo(newsid);
			List<News> news =newsMapper.selectByExample(newsExample);
//			System.out.println("news"+news);
			return news;
		}
		return null;
	}
	
	/**
	 * 获取topic对应的news列表
	 * **/
	public List<News> getTopicAllNews(String topicid){
		List<News> news = new ArrayList<News>();
		TopicDocumentRelationExample example = new TopicDocumentRelationExample();
		TopicDocumentRelationExample.Criteria criteria = example.createCriteria();
		criteria.andTopic_idEqualTo(topicid);
		//获取所有的新闻id
		List<TopicDocumentRelation> topicNewsRelation = documentRelationMapper.selectByExample(example);
//		System.out.println(topicNewsRelation);
		Iterator<TopicDocumentRelation> TopicNews = topicNewsRelation.iterator();
		//得到每一条新闻的详情
		while(TopicNews.hasNext()){
			TopicDocumentRelation topicDocumentRelation = TopicNews.next();
			int newsid = topicDocumentRelation.getNews_id();
			System.out.println("一条一条："+newsid);
			NewsExample newsExample = new NewsExample();
			NewsExample.Criteria criteriaNews = newsExample.createCriteria();
			criteriaNews.andNews_idEqualTo(newsid);
			example.setOrderByClause("news_time");
			List<News> temp =newsMapper.selectByExample(newsExample);
			news.addAll(temp);
		}
		
		return news;
	}
}
